package com.wing.common.utils;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 返回结果对象
 *
 * @author
 */
@Data
@NoArgsConstructor
@Accessors(chain = true)
public class JsonResult<T> extends Result implements Serializable {
    
    private static final long serialVersionUID = 1L;

    /**
     * html start
     */
    public static final String HTML_START = "<!DOCTYPE html><html><head lang=en'><meta charset='UTF-8'>"+
            "<meta content='width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no' name='viewport' />"+
            "<style type='text/css'>body,html,p,section,img,h1,h2,h3,div {margin: 0 !important;padding: 0 !important;}"+
            "img {max-width: 100% !important; width: 100% !important; height: auto;}p > span {line-height:35px;}"+
            "p {display:block;overflow:hidden;margin:5px;}</style></head><body>";

    /**
     * html end
     */
    public static final String HTML_END = "</body></html>";


    /**
     * 返回数据
     */
    private T data;


    JsonResult(int code, String msg){
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    JsonResult(int code, String msg, T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static JsonResult success(){
        return new JsonResult(JsonResult.Code.SUCCESS.getCode(), JsonResult.Code.SUCCESS.getDesc());
    }

    public static JsonResult success(String msg){
        return new JsonResult(JsonResult.Code.SUCCESS.getCode(), msg);
    }

    public static JsonResult success(Object data){
        return new JsonResult(JsonResult.Code.SUCCESS.getCode(), JsonResult.Code.SUCCESS.getDesc(), data);
    }

    public static JsonResult success(String msg, Object data){
        return new JsonResult(JsonResult.Code.SUCCESS.getCode(), msg, data);
    }

    public static JsonResult fail(String msg, Object data){
        return new JsonResult(Code.FAIL.getCode(), msg, data);
    }

    public static JsonResult fail(int code, String msg, Object data){
        return new JsonResult(code, msg, data);
    }

    public static JsonResult success(int code, String msg, Object data){
        return new JsonResult(code, msg, data);
    }

    public static JsonResult fail() {
        return new JsonResult(JsonResult.Code.FAIL.getCode(), JsonResult.Code.FAIL.getDesc());
    }

    public static JsonResult fail(String msg) {
        return new JsonResult(JsonResult.Code.FAIL.getCode(), msg);
    }

    public static JsonResult fail(int code, String msg) {
        return new JsonResult(code, msg);
    }

    public static JsonResult fail(Code code) {
        return new JsonResult(code.getCode(), code.getDesc());
    }

    public static JsonResult exception() {
        return new JsonResult(JsonResult.Code.EXCEPTION.getCode(), JsonResult.Code.EXCEPTION.getDesc());
    }

    public static JsonResult exception(String msg) {
        return new JsonResult(JsonResult.Code.EXCEPTION.getCode(), msg);
    }

    public static JsonResult exception(int code, String msg) {
        return new JsonResult(code, msg);
    }

    public static JsonResult exception(Exception ex) {
        return new JsonResult(JsonResult.Code.EXCEPTION.getCode(), JsonResult.Code.EXCEPTION.getDesc(), ex.getMessage());
    }

    public static JsonResult parameterError() {
        return new JsonResult(JsonResult.Code.PARAMETER_ERROR.getCode(), JsonResult.Code.PARAMETER_ERROR.getDesc());
    }

    public static JsonResult parameterError(String msg) {
        return new JsonResult(JsonResult.Code.PARAMETER_ERROR.getCode(), msg);
    }

    public static JsonResult loginError() {
        return new JsonResult(JsonResult.Code.LOGIN_ERROR.getCode(), JsonResult.Code.LOGIN_ERROR.getDesc());
    }

    public static JsonResult loginError(String msg) {
        return new JsonResult(JsonResult.Code.LOGIN_ERROR.getCode(), msg);
    }

    public static JsonResult error(int code, String msg) {
        return new JsonResult(code, msg);
    }

    public static JsonResult error(Code code) {
        return new JsonResult(code.getCode(), code.getDesc());
    }

    /**
     * 是否有数据
     *
     * @param result 结果
     * @return boolean
     */
    public static boolean hasData(JsonResult result) {

        //是否有结果
        if(!isSuccess(result)){
            return false;
        }

        //是否有数据
        if(result.data == null){
            return false;
        }

        //是否是集合
        if(result.data instanceof Collection){
            return CollUtil.isNotEmpty((Collection)result.data);
        }

        return true;
    }
}